package ui.nav;
import content.ContentManager;
import easelhx.display.Shape;
import easelhx.display.Stage;
import easelhx.display.Text;
import js.Dom.HtmlDom;
import js.DomCanvas;
import js.Lib;
import net.address.Address;
import params.GlobalParams;
import tools.ASprite;
/**
 * ...
 * @author PRASS
 */

class Nav extends ASprite 
{
	
	private var _view 		: HtmlDom;
	private var _this 		: JQuery;
	
	private var _bt_works 	: NavItem;
	private var _bt_about 	: NavItem;
	private var _bt_contact : NavItem;
	private var _bt_links 	: NavItem;
	
	private var _current	: NavItem;
	private var _old		: NavItem;
	private var _logo		: ASprite;
	
	
	public function new( view : HtmlDom, elem : JQuery ) 
	{
		super(elem);
		
		_view = view;
		
		_logo = new Logo(new JQuery("#logo"));
		_logo.dom.click( function (e) { onClick(e); } );
		
		_bt_works = new NavItem("#btWorks", "wCanvas", "WORKS");
		_bt_works.dom.css("letter-spacing", "4px");
		_bt_works.dom.click( function (e) { onClick(e); } );
		
		_bt_about = new NavItem("#btAbout", "aCanvas", "ABOUT");
		_bt_about.dom.css("letter-spacing", "5px");
		_bt_about.dom.click( function (e) { onClick(e); } );
		
		_bt_contact = new NavItem("#btContact", "cCanvas", "CONTACT");
		_bt_contact.dom.css("letter-spacing", "1.7px");
		_bt_contact.dom.css("margin-left", "-2px");
		_bt_contact.dom.click( function (e) { onClick(e); } );
		
		_bt_links = new NavItem("#btLinks", "lCanvas", "LINKS");
		_bt_links.dom.css("letter-spacing", "6px");
		_bt_links.dom.click( function (e) { onClick(e); } );
		
		Address.addCallBack( _handleSelect );
	}
	
	private function onClick( e ) : Void
	{
		if ( ContentManager.isMoving ) return;
			
		var hash : String = "";
		
		switch( e.target.id )
		{
			case "logo": 		hash = GlobalParams.ID_MAIN;
			case "btWorks": 	hash = GlobalParams.ID_WORKS;
			case "btAbout":		hash = GlobalParams.ID_ABOUT;
			case "btContact":	hash = GlobalParams.ID_CONTACT;
			case "btLinks": 	hash = GlobalParams.ID_LINKS;
		}
		
		Address.goto( hash );
	}
	
	// Referenced by Address
	private function _handleSelect( hash : String ) : Void
	{
		if ( _old != null ) _old.unselect();
		
		switch( hash )
		{
			case "":
				clear();
				return;
			case GlobalParams.ID_MAIN:
				clear();
				return;
			case GlobalParams.ID_WORKS: 	_current = _bt_works;
			case GlobalParams.ID_ABOUT: 	_current = _bt_about;
			case GlobalParams.ID_CONTACT:	_current = _bt_contact;
			case GlobalParams.ID_LINKS: 	_current = _bt_links;
		}
		
		// safety
		if (_current == null) return;
		
		_current.select();
		_old = _current;
	}
	
	private function clear() : Void
	{
		if ( _old != null )
		{
			_old.unselect();
			_old = null;
		}
		
		if ( _current != null )
		{
			_current.unselect();
			_current = null;
		}
	}
	
}